package com.tianji.learning.controller;

import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.dto.LearningPlanDTO;
import com.tianji.learning.service.LearningLessonService;
import com.tianji.learning.vo.LearningLessonVO;
import com.tianji.learning.vo.LearningPlanPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "我的课表相关接口")
@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
public class LearningLessonController {

    private final LearningLessonService lessonService;

    /**
     * 分页查询我的课表
     * @param query
     * @return
     */
    @ApiOperation("查询我的课表，排序字段 latest_learn_time:学习时间排序，create_time:购买时间排序")
    @GetMapping("/page")
    public PageDTO<LearningLessonVO> queryMyLessons(PageQuery query) {
        return lessonService.queryMyLessons(query);
    }


    /**
     * 查询我正在学习的课程
     * @return
     */
    @GetMapping("/now")
    @ApiOperation("查询我正在学习的课程")
    public LearningLessonVO queryMyCurrentLesson() {
        return lessonService.queryMyCurrentLesson();
    }


    /**
     * 校验当前用户是否可以学习当前课程
     * @param courseId 课程id
     * @return lessonId，如果是报名了则返回lessonId，否则返回空
     */
    @GetMapping("/{courseId}/valid")
    @ApiOperation("检查课程是否有效")
    public Long isLessonValid(@PathVariable("courseId") Long courseId) {
        return lessonService.isLessonValid(courseId);
    }

    /**
     * 查询用户课表中指定课程的状态
     * @param courseId
     * @return
     */
    @ApiOperation("查询用户课表中指定课程状态")
    @GetMapping("{courseId}")
    public LearningLessonVO queryLessonByCourseId(@PathVariable("courseId") Long courseId) {
        return lessonService.queryLessonByCourseId(courseId);
    }

    /**
     * 创建学习计划
     * @param planDTO
     */
    @ApiOperation("创建学习计划")
    @PostMapping("/plans")
    public void createLearningPlans(@RequestBody @Validated LearningPlanDTO planDTO) {
        lessonService.createLearningPlan(planDTO);
    }

    /**
     * 分页查询我的学习计划
     * @param query
     * @return
     */
    @ApiOperation("查询我的学习计划")
    @GetMapping("/plans")
    public LearningPlanPageVO queryMyPlans(PageQuery query) {
        return lessonService.queryMyPlans(query);
    }

}